Ext.ns('SM.GroupManage')
/**
 * 系统管理-组管理
 * @class SM.GroupManage
 * @extends Ext.Panel
 */
SM.GroupManage =Ext.extend(Ext.Panel,{
	 closable: true
	 ,layout:'border'
   	 ,width:'80%'
   	 ,initComponent:function(){
   	 	 var operateSm=new Ext.grid.CheckboxSelectionModel({
							dataIndex: "id",
			                sortable: false,
			                singleSelect:false,
			                width:20
						})
		var taskSm=new Ext.grid.CheckboxSelectionModel({
							dataIndex: "id",
			                sortable: false,
			                singleSelect:false,
			                width:20
						});
		var ruleSm=new Ext.grid.CheckboxSelectionModel({
							dataIndex: "id",
			                sortable: false,
			                singleSelect:false,
			                width:20
						})
	     this.items =[
	     	{xtype:'treepanel'
	    	,title:'用户组树形结构'
   			,region : "west"
   			,autoScroll:true
   			,split:true
   			,contextmenu:{
	    	    xtype:'menu'
	    	    ,items:[{text:'添加组',handler:function(item,e){
	    	    	          SM.GroupManage.addGroup(this.ownerCt.node.parentNode,'添加组');
	    	    		}}
	    	    		,{text:'添加下级组',handler:function(item,e){
	    	    	          SM.GroupManage.addGroup(this.ownerCt.node,'添加下级组');
	    	    		}}
	    	    		,{text:'删除组',handler:function(item,e){
	    	    		       SM.GroupManage.deleteGroup(this.ownerCt.node);
	    	    		}}]
   			}
	        ,listeners:{
	        	destroy:function(t){
	        		if (t.contextmenu.render) {
	        			t.contextmenu.destroy()
	        		}
	        	}
	        	,'contextmenu':function(node,e){
	            	var owerTree =node.getOwnerTree();
	            	if (!owerTree.contextmenu.render){
	            		owerTree.contextmenu=Ext.create(owerTree.contextmenu);
	            	}
	            	owerTree.contextmenu.node =node;
	            	owerTree.contextmenu.showAt(e.xy);
	            	
	            }
	            ,'click':function(node,e) {
	            	var me =this;
	            	var myMask = new Ext.LoadMask(node.getOwnerTree().ownerCt.getEl(), {msg:"请稍候,正在加载数据中..."});
			        myMask.show();
	            	Ext.Ajax.request({
	            		url:'group_getInfoByGroupId.action?smGroup.groupId='+node.id
	            		,success:function(request,opts){
	            		   var obj =Ext.decode(request.responseText);
	            		   //得到右侧界面
	            		   var panelinfo =me.ownerCt.getComponent(1);
	            		   //把groupid放入右侧界面上
	            		   panelinfo.groupId=node.id;
	            		   panelinfo.node=node;
	            		   //填充信息页面
	            		   var groupinfo =panelinfo.getComponent(0);
	            		   var groupinfoValue =obj.root[0];
	            		   for(var i=0;i<groupinfo.items.items.length;i++){
	            		   	groupinfo.items.items[i].originalValue =groupinfoValue[groupinfo.items.items[i].name];
	            			  groupinfo.items.items[i].setValue(groupinfoValue[groupinfo.items.items[i].name]);
	            		   }
	            		   //操作员
	            		   var ops =panelinfo.getComponent(1).getComponent(0).store
	            		   ops.loadData({root:obj.root[1]});
	            		   ops.proxy.setUrl('group_getStaffGroupRel.action?smGroupStaffRel.groupId='+node.id,true);
	            		   //稽核任务
	            		   var tasks =panelinfo.getComponent(2).getComponent(0).store;
	            		   tasks.loadData({root:obj.root[2]});
	            		   tasks.proxy.setUrl('group_getTaskGroupRel.action?smGroupTaskRel.groupId='+node.id,true);
	            		   
	            		   //角色
	            		   var rules =panelinfo.getComponent(3).getComponent(0).store;
	            		   rules.loadData({root:obj.root[3]});
	            		   rules.proxy.setUrl('group_getRuleGroupRel.action?smGroupRuleRel.groupId='+node.id,true);
	            		   
	            		   myMask.hide();
	            		}
	            	   ,failure:function(){
	            	   		myMask.hide();
	            	   }
	            	})
	            }
	        }
   			,loader:{
	    	   dataUrl:'group_getGroups.action'
	    	  ,nodeParameter:'smGroup.parentGroupId'
	    	  ,root:'root'
   			}
   			,root:new Ext.tree.AsyncTreeNode({
							id:'0',  
					        text: '所有资源' ,
					        leaf:false,
					        expended:true
						 })
	        ,width:'20%'
   			,rootVisible:false
	     },
	     	{xtype:'panel'
	    	 ,title:'用户组详细信息'
	    	 ,split:true
	    	 ,listeners:{
	    	    single:true
	    	    ,'afterlayout':function(comp){
	    	          var setarray =[comp.items.itemAt(1)
	    	        	  ,comp.items.itemAt(2)
	    	        	  ,comp.items.itemAt(3)];
	    	          setarray[0].on('expand',function(){
	    	        	  setarray[1].collapse();
	    	        	  setarray[2].collapse();
	    	          });
	    	          setarray[1].on('expand',function(){
	    	        	  setarray[0].collapse();
	    	        	  setarray[2].collapse();
	    	          });
	    	          setarray[2].on('expand',function(){
	    	        	  setarray[1].collapse();
	    	        	  setarray[0].collapse();
	    	          });
	    	          setarray[1].collapse();
	    	          setarray[2].collapse();
	    	    }
	    	}
	        ,autoScroll:true
   		    ,region : "center"
   		    ,frame:true
   		    ,defaults:{	
				  bodyStyle:'padding:3px'
				}
			,clearData:function(){
				  delete this.groupId;
				  delete this.node;
			      this.items.each(function(item,index,length){
			           if (index ==0) {
			           	item.getForm().items.each(function(f){
           							f.setRawValue("");
    						 });
			           } else {
			           	  item.getComponent(0).getStore().removeAll();
			           }
			           return true;
			           
			      });
			}
   		    ,items:[
   		    	{xtype:'form'
   		    	,width:600
   		    	,buttonAlign:'center'
   		    	,buttons:[{text:'保存',handler:function(){
   		    		   var params="";
   		    		   var items =this.ownerCt.ownerCt.items.items;
   		    		   var groupId=this.ownerCt.ownerCt.ownerCt.groupId;
   		    		   var node =this.ownerCt.ownerCt.ownerCt.node;
   		    		   if (!Ext.isDefined(groupId)){
   		    	       		Ext.Msg.alert('提示','请选点击左边树节点加载数据后再进行此操作！');
   		    	       		return;
   		    	       	}
   		    		   var changeflag=false;
   		    	       for (var i =0;i<items.length;i++) {
   		    	       	if (!items[i].isValid()){
	        	       	    	Ext.Msg.alert('提示',items[i].fieldLabel+items[i].getActiveError());
	        	       	    	return ;
	    	    	        }
	    	    	     if (items[i].isDirty()) {
	    	    	     	changeflag =true;
	    	    	     }
   		    	       	params =params+'&smGroup.'+items[i].name+'='+items[i].getValue();
   		    	       }
   		    	       if (!changeflag) {
   		    	       		return;
   		    	       }
   		    	       params =params.substring(1);
   		    	       Ext.Ajax.request({
   		    	       	url:'group_updateGroup.action'
   		    	       	,params:encodeURI(params)
   		    	       	,method:'post'
   		    	       	,success:function(response,options){
   		    	       		Ext.Msg.alert('提示','保存成功');
   		    	       		node.parentNode.reload(function(n){
   		    	       		       n.findChild("id",groupId).select();
   		    	       		});
   		    	       	}
   		    	       });
   		    	}}]
   		    	,items:[{
   		    		xtype:'textfield'
   		    		,name:'groupId'
   		    		,hidden:true
   		    	},{
   		    		xtype:'textfield'
   		    		,fieldLabel:'组名称'
   		    		,allowBlank:false
   		    		,maxLength:200
   		    		,width:'400'
   		    		,name:'groupName'
   		    	},{
   		    		xtype:'textarea'
   		    		,fieldLabel:'组描述'
   		    		,maxLength:200
   		    		,width:'400'
   		    		,allowBlank:false
   		    		,name:'groupDesc'
   		    	},{
   		    		xtype:'numberfield'
   		    		,fieldLabel:'序号'
   		    		,width:'400'
   		    		,maxLength:10
   		    		,allowBlank:false
   		    		,name:'sortNo'
   		    	},{xtype:'textfield',hidden:true,name:'parentGroupId'}]
   		    }
   		       ,{xtype:'fieldset'
   		       ,collapsible: true
		       ,titleCollapse:true
		       ,title:'操作员关联'
		       ,height:'200'
		       ,width:'90%'
		       ,items:[{
		    	   xtype:'grid'
		    	   ,loadMask:'正在加载中,请稍候...'
		    	   ,tbar:[{text:'关联操作员'
		    	   		   ,iconCls:'ico-task-add'
		    	   		   ,handler:function(){
		    	   		   	   //要得到数据最到的sort
		    	   		   	   var groupId=this.ownerCt.ownerCt.ownerCt.ownerCt.groupId;
		    	   		   	   if (!Ext.isDefined(groupId)){
		   		    	       		Ext.Msg.alert('提示','请选点击左边树节点加载数据后再进行此操作！');
		   		    	       		return;
   		    	      		 	} else {
		    	                new SM.GroupManage.serachUser({groupId:groupId
		    	                                              ,parentGrid:this.ownerCt.ownerCt}).show();
   		    	      		 	}
		    	   			  }
		    	   		    }
		    	   		   ,'-'
		    	   		   ,{text:'取消关联'
		    	   		     ,iconCls:'ico-task-delete'
		    	   		     ,handler:function(){
		    	   		     	var grid =this.ownerCt.ownerCt;
		    	   		     	var recs =grid.getSelectionModel().getSelections();
		    	   		     	if (recs.length==0){
		             				Ext.Msg.alert('提示','请选择一条记录！');
		             			} else {
		             				Ext.Msg.confirm('确定','确认删除这'+recs.length+'记录？'
		             				              ,function(n){
		             				        		if (n=='yes') {
			             				        		var ids ='';
							             				for (var a=0;a<recs.length;a++) {
							             					ids =ids +','+recs[a].get('id');
							             				}
							             				ids =ids.substring(1);
							             				Ext.Ajax.request({
							             				        url:'group_delStaffGroupRel.action'
							             				        ,params:{ids:ids}
							             				        ,success:function(request,conn) {
							             				        	grid.store.remove(recs);
							             				        }
							             				});
							             				        }
							             				});
		             				
		             								}
		    	   		    					 }
		    	   		    }
		    	   		   ,'-'
		    	   		   ,{text:'更新关联'
		    	             ,iconCls:'ico-task-update'
		    	             ,handler:function(){
		    	             	this.ownerCt.ownerCt.updateStaffGroupRel();
		    	             }
		    	             }]
		    	   ,height:'200'
		    	   ,viewConfig:{
		    	      forceFit: true
		    	   }
		           ,frame:true
		           ,store:{
		        	   xtype:'jsonstore'
		        	   ,url:'1'
		        	   ,fields:['id','staffId','staffName','isGroupleader','sortNo','staffRealId','groupId']
		        	   ,root:'root'
		        	   ,sortInfo:{field:'sortNo'}
		           }
		           ,getMaxSortNo:function(){
		              var maxSort =0;
		           	  this.store.each(function(item,index,legnth){
		    	   		   	           if (maxSort <item.get('sortNo')){
		    	   		   	           	 maxSort = item.get('sortNo');
		    	   		   	           }
		    	   		   	           return true;
		    	   		   	    });
		    	   	   return maxSort;	   	    
		           }
		           ,updateStaffGroupRel:function(){
		           	    var me =this;
		           	    var recs =me.getSelectionModel().getSelections();
		           	    var store =me.store;
		             	if (recs.length==0){
		             		Ext.Msg.alert('提示','请选择一条记录！');
		             	} else if (recs.length >1){
		             		Ext.Msg.alert('提示','只能更新一条记录,请只选择一条记录');
		             	} else {
		             		var rec =recs[0];
		             		new Ext.Window({
		             		      modal:true
	    	    	        	  ,title:'修改操作员与组关联关系'
	    	    	        	  ,width:340
	    	    	        	  ,height:200
	    	    	        	  ,layout:'form'
	    	    	        	  ,buttons:[{text:'确定',handler:function(){
	    	    	        	  	   var win =this.ownerCt.ownerCt;
	    	    	        	       var items =this.ownerCt.ownerCt.items.items;
	    	    	        	       var params="";
	    	    	        	       
	    	    	        	       var isGroupleader =''; 
	    	    	        	       var sortNo ='';
	    	    	        	       var changeflag =false;
	    	    	        	       for (var i=0;i<items.length;i++){
	    	    	        	       	    if (!items[i].isValid()){
	    	    	        	       	    	Ext.Msg.alert('提示',items[i].name+items[i].getActiveError());
	    	    	        	       	    	return ;
	    	    	        	       	    }
	    	    	        	       	    if (items[i].isDirty()){
	    	    	        	       	    	changeflag =true;
	    	    	        	       	    }
	    	    	        	       	    if (items[i].isupdate) {
	    	    	        	       	      params =params+'&smGroupStaffRel.'+items[i].name+'='+items[i].getValue();
	    	    	        	       	    }
	    	    	        	       	    if (items[i].name =='sortNo'){
	    	    	        	       	    	sortNo =items[i].getValue();
	    	    	        	       	    }
	    	    	        	       	    if (items[i].name =='isGroupleader'){
	    	    	        	       	    	isGroupleader =items[i].getValue();
	    	    	        	       	    }
	    	    	        	       	    
	    	    	        	       }
	    	    	        	       //没有更改直接返回
	    	    	        	       if (!changeflag) {
	    	    	        	       	 win.close();
	    	    	        	       	 return;
	    	    	        	       }
	    	    	        	       params =params.substring(1);
	    	    	        	       //查询是否已经有组长了，有组长则提醒
	    	    	        	       if (isGroupleader ==1){
	    	    	        	       	   var flag =false;
	    	    	        	       	   store.each(function(item){
	    	    	        	       	        if (item.get('isGroupleader') ==1&&item.get('id')!=rec.get('id')) {
	    	    	        	       	           	 flag =true;
	    	    	        	       	           	 return false;
	    	    	        	       	        }
	    	    	        	       	        return true;
	    	    	        	       	   });
	    	    	        	       	   if (flag) {
	    	    	        	       	   	Ext.Msg.alert('提示','已经有组长存在,不允许再设置为组长！');
	    	    	        	       	   	return;
	    	    	        	       	   }
	    	    	        	       }
	    	    	        	       Ext.Ajax.request({
					   		    	       	url:'group_updateStaffGroupRel.action'
					   		    	       	,params:encodeURI(params)
					   		    	       	,method:'post'
					   		    	       	,success:function(response,options){
					   		    	       		rec.set('sortNo',sortNo);
					   		    	       		rec.set('isGroupleader',isGroupleader);
					   		    	       		rec.commit();
					   		    	       		store.singleSort('sortNo','ASC');
					   		    	       		win.close();
					   		    	       	}
   		    	      				 });
	    	    	        	  }}]
	    	    	        	  ,labelAlign:'right'
	    	    	        	  ,items:[
	    	    	        	       {
	    	    	        	       	  xtype:'textfield'
						   		    		,fieldLabel:'id'
						   		    		,width:'200'
						   		    		,hidden:true
						   		    		,name:'id'
						   		    		,isupdate:true
						   		    		,value:rec.get('id')
	    	    	        	       },{
	    	    	        	       	  xtype:'textfield'
						   		    		,fieldLabel:'staffId'
						   		    		,width:'200'
						   		    		,hidden:true
						   		    		,isupdate:true
						   		    		,name:'staffId'
						   		    		,value:rec.get('staffRealId')
	    	    	        	       },{
	    	    	        	       	  xtype:'textfield'
						   		    		,fieldLabel:'groupId'
						   		    		,width:'200'
						   		    		,hidden:true
						   		    		,isupdate:true
						   		    		,name:'groupId'
						   		    		,value:rec.get('groupId')
	    	    	        	       },{  style:{marginTop:'10px'}
	    	    	        	  	        ,labelStyle:"margin-top:10px"
						   		    		,xtype:'textfield'
						   		    		,fieldLabel:'操作员标识'
						   		    		,width:'200'
						   		    		,readOnly:true
						   		    		,value:rec.get('staffId')
						   		    	},{
						   		    		xtype:'textfield'
						   		    		,fieldLabel:'操作员名称'
						   		    		,width:'200'
						   		    		,readOnly:true
						   		    		,name:'staffName'
						   		    		,value:rec.get('staffName')
						   		    	},{
						   		    		xtype:'combo'
						   		    		,fieldLabel:'是否组长'
						   		    		,width:'200'
						   		    		,name:'isGroupleader'
						   		    		,hiddenName:'isGroupleader'
						   		    		,triggerAction:'all'
						   		    		,editable:false
						   		    		,allowBlank:false
						   		    		,isupdate:true
						   		    		,store:[['1','是'],['0','否']]
						   		    		,value:rec.get('isGroupleader')
						   		    	},{
						   		    		xtype:'numberfield'
						   		    		,fieldLabel:'序号'
						   		    		,width:'200'
						   		    		,allowBlank:false
						   		    		,name:'sortNo'
						   		    		,maxLength:10
						   		    		,isupdate:true
						   		    		,value:rec.get('sortNo')
						   		    	}]
	    	    	          }).show();
		             	}
		           }
		           ,listeners:{
		           	   afterrender:function(a) {
		           	   	   a.on('dblclick',a.updateStaffGroupRel);
		           	   }
		           }
		           ,sm:operateSm
		           ,columns:[operateSm,{header:'操作员标识',dataIndex:'staffId'}
		    	             ,{header:'操作员名称',dataIndex:'staffName'}
		    	             ,{header:'是否组长',dataIndex:'isGroupleader',renderer:function(value){
		    	             	  if (value ==0) {
		    	             	  	return '否'
		    	             	  }
		    	             	  return '是'
		    	             }}
		    	             ,{header:'序号',dataIndex:'sortNo'}]
		    	   
		       }]
   		       }
   		       ,{xtype:'fieldset'
   		       ,collapsible: true
		       ,titleCollapse:true
		       ,title:'稽核任务关联'
		       ,height:'200'
		       ,width:'90%'
		       ,items:[{
		    	   xtype:'grid'
		    	   ,loadMask:'正在加载中,请稍候...'
		    	   ,tbar:[{text:'关联稽核任务'
		    	           ,iconCls:'ico-task-add'
		    	           ,handler:function(){
		    	           	//要得到数据最到的sort
		    	   		   	   var groupId=this.ownerCt.ownerCt.ownerCt.ownerCt.groupId;
		    	   		   	   if (!Ext.isDefined(groupId)){
		   		    	       		Ext.Msg.alert('提示','请选点击左边树节点加载数据后再进行此操作！');
		   		    	       		return;
   		    	      		 	} 
   		    	      		 	var parentGroupId= this.ownerCt.ownerCt.ownerCt.ownerCt.node.parentNode.id;
		    	                new SM.GroupManage.serachTask({groupId:groupId
		    	                                              ,parentGrid:this.ownerCt.ownerCt
		    	                                              ,parentGroupId:parentGroupId}).show();
   		    	      		 	
		    	           }},'-'
		    	           ,{text:'取消关联',iconCls:'ico-task-delete'
		    	             ,handler:function(){
		    	   		     	var grid =this.ownerCt.ownerCt;
		    	   		     	var recs =grid.getSelectionModel().getSelections();
		    	   		     	if (recs.length==0){
		             				Ext.Msg.alert('提示','请选择一条记录！');
		             			} else {
		             				Ext.Msg.confirm('确定','确认删除这'+recs.length+'记录？'
		             				              ,function(n){
		             				        		if (n=='yes') {
			             				        		var ids ='';
							             				for (var a=0;a<recs.length;a++) {
							             					ids =ids +','+recs[a].get('id');
							             				}
							             				ids =ids.substring(1);
							             				Ext.Ajax.request({
							             				        url:'group_delTaskGroupRel.action'
							             				        ,params:{ids:ids}
							             				        ,success:function(request,conn) {
							             				        	grid.store.remove(recs);
							             				        }
							             				});
							             				        }
							             				});
		             				
		             								}
		    	   		    	}
		    	   		    	}]
		    	   ,height:'200'
		    	   ,viewConfig:{
		    	      forceFit: true
		    	   }
		    	   ,sm:taskSm
		           ,frame:true
		           ,store:{
		        	   xtype:'jsonstore'
		        	   ,url:'1'
		        	   ,fields:['id','taskName','taskDesc','taskCode']
		        	   ,root:'root'
		           }
		    	   ,columns:[taskSm,{header:'稽核任务名称',dataIndex:'taskName'}
		    	             ,{header:'描述',dataIndex:'taskDesc'}]
		       }]
   		       }
   		       ,{xtype:'fieldset'
   		       ,collapsible: true
		       ,titleCollapse:true
		       ,title:'角色关联'
		       ,height:'200'
		       ,width:'90%'
		       ,items:[{
		    	   xtype:'grid'
		    	   ,loadMask:'正在加载中,请稍候...'
		    	   ,tbar:[{text:'关联角色'
		    	           ,iconCls:'ico-task-add'
		    	           ,handler:function(){
		    	           	//要得到数据最到的sort
		    	   		   	   var groupId=this.ownerCt.ownerCt.ownerCt.ownerCt.groupId;
		    	   		   	   if (!Ext.isDefined(groupId)){
		   		    	       		Ext.Msg.alert('提示','请选点击左边树节点加载数据后再进行此操作！');
		   		    	       		return;
   		    	      		 	} 
		    	                new SM.GroupManage.serachRule({groupId:groupId
		    	                                              ,parentGrid:this.ownerCt.ownerCt}).show();
   		    	      		 	
		    	           }},'-'
		    	           ,{text:'取消关联',iconCls:'ico-task-delete'
		    	             ,handler:function(){
		    	   		     	var grid =this.ownerCt.ownerCt;
		    	   		     	var recs =grid.getSelectionModel().getSelections();
		    	   		     	if (recs.length==0){
		             				Ext.Msg.alert('提示','请选择一条记录！');
		             			} else {
		             				Ext.Msg.confirm('确定','确认删除这'+recs.length+'记录？'
		             				              ,function(n){
		             				        		if (n=='yes') {
			             				        		var ids ='';
							             				for (var a=0;a<recs.length;a++) {
							             					ids =ids +','+recs[a].get('id');
							             				}
							             				ids =ids.substring(1);
							             				Ext.Ajax.request({
							             				        url:'group_delRuleGroupRel.action'
							             				        ,params:{ids:ids}
							             				        ,success:function(request,conn) {
							             				        	grid.store.remove(recs);
							             				        }
							             				});
							             				        }
							             				});
		             				
		             								}
		    	   		    					 }}]
		    	   ,height:'200'
		    	   ,viewConfig:{
		    	      forceFit: true
		    	   }
		           ,frame:true
		           ,store:{
		        	   xtype:'jsonstore'
		        	   ,url:'1'
		        	   ,fields:['id','roleDesc','roleId','roleName']
		        	   ,root:'root'
		           }
		           ,sm:ruleSm
		    	   ,columns:[ruleSm,{header:'角色名称',dataIndex:'roleName'}
		    	             ,{header:'角色描述',dataIndex:'roleDesc'}]
		       }]
   		       }]
	     }];
	     SM.GroupManage.superclass.initComponent.call(this);
   	 }		 
});
/**
 * 操作搜索界面
 * @class SM.GroupManage.serachUser
 * @extends Ext.Window
 */
SM.GroupManage.serachUser =Ext.extend(Ext.Window,{
				layout:'form',
                width:650,
                height:395,
                modal:true,
                closeAction:'close',
                resizable : true,
                plain: true,
                limit:25,
                title:'操作员查询',
                initComponent: function(){
	                var mystore =new Ext.data.JsonStore({
	                	 totalProperty:'totalProperty'
	                	 ,url:'group_getStaffByGroupId.action?groupId='+this.groupId
	                	 ,fields:['id','staffId','staffName']
	                	 ,root:'root'
	                });
	                this.store =mystore;
	                var sm =new Ext.grid.CheckboxSelectionModel({
				                sortable: false,
				                singleSelect:false,
				                width:20
							})
	                this.tbar = [{xtype:'label',text:'地市：'},{
	                	      ref:'../typecombo',
							  xtype:'combo',
		                 	  hiddenName:'type',
		                 	  width:200,
		                 	  store:[["'0','1'",'全部']],
							   displayField:'typeDisplay',
					     	   valueField:'typeValue',
					           editable:false,
					           mode : 'local',
					           value:"'0','1'",
					           triggerAction: 'all'
						},'-',{xtype:'label',text:'操作员名称：'},{
							 xtype:'textfield',
							 ref:'../valuetext',
		                 	 name:'staffName',
		                 	 width:200,
		                 	 emptyText:'请输入操作员名称',
		                 	 listeners:{
		                 	 	'specialkey':function(f,e) {
		                 	 		if(e.getKey() == e.ENTER){
		                 	 		    var store =this.ownerCt.ownerCt.items.items[0].store;
			                     	    var combotext = this.ownerCt.items.items[1];
			                     	    store.baseParams ={staffName:this.getValue(),
			          	                           type:combotext.getValue()};
			          	                store.load({params:{start:0, limit:this.ownerCt.ownerCt.limit}});
		                 	 		}
		                 	 	}
		                 	 }
						
						},'-',{	text: '查询',
							xtype:'button',
							iconCls:'search',
	                     	listeners:{
	                     		click:function(n) {
		                     	    var store =this.ownerCt.ownerCt.items.items[0].store;
			                     	    var combotext = this.ownerCt.items.items[1];
			                     	    store.baseParams ={staffName:this.ownerCt.items.items[4].getValue(),
			          	                           type:combotext.getValue()};
			          	                store.load({params:{start:0, limit:this.ownerCt.ownerCt.limit}});
	                     		}
	                     	}
			                     	
						}];	
	                this.items= [
	                    {
	                     xtype:'grid',
	                     sm:sm,
						 loadMask:'正在加载数据中，请稍候...'	,
	                	 anchor:'100%',
	                	 height:300,
	                     bbar: new Ext.PagingToolbar({
					            pageSize: this.limit,
					            displayInfo: true,
					            store:mystore,
					            displayMsg: '显示的操作员 {0} - {1} 总共 {2}',
					            emptyMsg: "没有找到操作员 "
	       					}),	
	                      viewConfig: {
	                       forceFit: true
	                      },
	                      columns:[sm,{header:'操作员ID',dataIndex: 'staffId'},
	                      	       {header:'名字',dataIndex: 'staffName'}
	                      	       ],
	                      store:mystore
	                	}],
	                this.buttons= [{
	                    text:'确认',
	                    listeners:{
	                     		click:function(n) {
	                     			var recs =this.ownerCt.ownerCt.getComponent(0).getSelectionModel().getSelections();
	                     			var staffIds ='';
	                     			var isGroupleaders='';
	                     			var sortNos='';
	                     			var grid =this.ownerCt.ownerCt.parentGrid;
	                     			var maxSort =grid.getMaxSortNo();
	                     			var pstore =grid.getStore();
	                     			for (var a =0;a <recs.length;a++) {
	                     				staffIds =staffIds+','+recs[a].get('id');
	                     				isGroupleaders =isGroupleaders+','+'0';
	                     				sortNos =sortNos+','+(maxSort+a+1);
	                     			}
	                     			if (recs.length!=0) {
	                     				staffIds =staffIds.substring(1)
	                     				isGroupleaders =isGroupleaders.substring(1);
	                     				sortNos =sortNos.substring(1);
	                     				var myMask = new Ext.LoadMask(Ext.getBody(), {msg:"正加添加中，请稍候..."});
	                     				myMask.show(this.ownerCt.ownerCt);
	                     				var me =this.ownerCt.ownerCt;
	                     				Ext.Ajax.request({
	                     				      url:'group_addStaffGroupRel.action'
	                     				      ,params:{staffIds:staffIds
	                     				               ,isGroupleaders:isGroupleaders
	                     				               ,sortNos:sortNos
	                     				               ,groupId:this.ownerCt.ownerCt.groupId}
	                     				      ,success:function(request,conn) {
	                     				      	 myMask.hide()
	                     				      	 me.close();
	                     				      	 Ext.Msg.alert('提示','添加成功');
	                     				      	 pstore.load();
	                     				      }
	                     				      ,failure:function(){
	                     				      	myMask.hide()
	                     				      }
	                     				});
	                     			} else {
	                     				this.ownerCt.ownerCt.close();
	                     			}
	                     		}
	                    }
	                },{
	                 text:'关闭',
	                 listeners:{
	                 	click:function(n) {
	                 		this.ownerCt.ownerCt.close();
	                 	}
	                 }
	                }]
	                SM.GroupManage.serachUser.superclass.initComponent.call(this);
      }
});

/**
 * 稽核任务搜索
 * @class SM.GroupManage.serachTask
 * @extends Ext.Window
 */
SM.GroupManage.serachTask =Ext.extend(Ext.Window,{
				layout:'form',
                width:650,
                height:395,
                modal:true,
                closeAction:'close',
                resizable : true,
                plain: true,
                limit:25,
                title:'稽核任务查询',
                initComponent: function(){
	                var mystore =new Ext.data.JsonStore({
	                	 totalProperty:'totalProperty'
	                	 ,url:'group_getTaskByGroupId.action?groupId='+this.groupId+'&parentGroupId='+this.parentGroupId
	                	 ,fields:['taskCode','taskName','taskDesc']
	                	 ,root:'root'
	                });
	                this.store =mystore;
	                var sm =new Ext.grid.CheckboxSelectionModel({
				                sortable: false,
				                singleSelect:false,
				                width:20
							})
	                this.tbar = [{xtype:'label',text:'稽核任务名称：'},{
							 xtype:'textfield',
							 ref:'../valuetext',
		                 	 name:'taskName',
		                 	 width:250,
		                 	 emptyText:'请输入稽核任务名称',
		                 	 listeners:{
		                 	 	'specialkey':function(f,e) {
		                 	 		if(e.getKey() == e.ENTER){
		                 	 		    var store =this.ownerCt.ownerCt.items.items[0].store;
			                     	    store.baseParams ={taskName:this.getValue()};
			          	                store.load({params:{start:0, limit:this.ownerCt.ownerCt.limit}});
		                 	 		}
		                 	 	}
		                 	 }
						
						},'-',{	text: '查询',
							xtype:'button',
							iconCls:'search',
	                     	listeners:{
	                     		click:function(n) {
		                     	    var store =this.ownerCt.ownerCt.items.items[0].store;
			                     	    var combotext = this.ownerCt.items.items[1];
			                     	    store.baseParams ={taskName:this.ownerCt.items.items[1].getValue()};
			          	                store.load({params:{start:0, limit:this.ownerCt.ownerCt.limit}});
	                     		}
	                     	}
			                     	
						}];	
	                this.items= [
	                    {
	                     xtype:'grid',
	                     sm:sm,
						 loadMask:'正在加载数据中，请稍候...'	,
	                	 anchor:'100%',
	                	 height:300,
	                     bbar: new Ext.PagingToolbar({
					            pageSize: this.limit,
					            displayInfo: true,
					            store:mystore,
					            displayMsg: '显示的稽核任务 {0} - {1} 总共 {2}',
					            emptyMsg: "没有找到稽核任务 "
	       					}),	
	                      viewConfig: {
	                       forceFit: true
	                      },
	                      columns:[sm,{header:'稽核任务名称',dataIndex: 'taskName'},
	                      	       {header:'稽核任务描述',dataIndex: 'taskDesc'}],
	                      store:mystore
	                	}],
	                this.buttons= [{
	                    text:'确认',
	                    listeners:{
	                     		click:function(n) {
	                     			var recs =this.ownerCt.ownerCt.getComponent(0).getSelectionModel().getSelections();
	                     			var taskCodes ='';
	                     			var pstore =this.ownerCt.ownerCt.parentGrid.getStore();
	                     			for (var a =0;a <recs.length;a++) {
	                     				taskCodes =taskCodes+','+recs[a].get('taskCode');
	                     			}
	                     			if (recs.length!=0) {
	                     				taskCodes =taskCodes.substring(1)
	                     				var myMask = new Ext.LoadMask(Ext.getBody(), {msg:"正加添加中，请稍候..."});
	                     				myMask.show(this.ownerCt.ownerCt);
	                     				var me =this.ownerCt.ownerCt;
	                     				Ext.Ajax.request({
	                     				      url:'group_addTaskGroupRel.action'
	                     				      ,params:{taskCodes:taskCodes
	                     				               ,groupId:this.ownerCt.ownerCt.groupId}
	                     				      ,success:function(request,conn) {
	                     				      	 myMask.hide()
	                     				      	 me.close();
	                     				      	 Ext.Msg.alert('提示','添加成功');
	                     				      	 pstore.load();
	                     				      }
	                     				      ,failure:function(){
	                     				      	myMask.hide()
	                     				      }
	                     				});
	                     			} else {
	                     				this.ownerCt.ownerCt.close();
	                     			}
	                     		}
	                    }
	                },{
	                 text:'关闭',
	                 listeners:{
	                 	click:function(n) {
	                 		this.ownerCt.ownerCt.close();
	                 	}
	                 }
	                }]
	                SM.GroupManage.serachTask.superclass.initComponent.call(this);
      }
});

/**
 * 角色搜索
 * @class SM.GroupManage.serachRule
 * @extends Ext.Window
 */
SM.GroupManage.serachRule =Ext.extend(Ext.Window,{
				layout:'form',
                width:650,
                height:395,
                modal:true,
                closeAction:'close',
                resizable : true,
                plain: true,
                limit:25,
                title:'角色查询',
                initComponent: function(){
	                var mystore =new Ext.data.JsonStore({
	                	 totalProperty:'totalProperty'
	                	 ,url:'group_getRuleByGroupId.action?groupId='+this.groupId
	                	 ,fields:['roleId','roleName','roleDesc']
	                	 ,root:'root'
	                });
	                this.store =mystore;
	                var sm =new Ext.grid.CheckboxSelectionModel({
				                sortable: false,
				                singleSelect:false,
				                width:20
							})
	                this.tbar = [{xtype:'label',text:'角色名称：'},{
							 xtype:'textfield',
							 ref:'../valuetext',
		                 	 name:'roleName',
		                 	 width:250,
		                 	 emptyText:'请输入角色名称',
		                 	 listeners:{
		                 	 	'specialkey':function(f,e) {
		                 	 		if(e.getKey() == e.ENTER){
		                 	 		    var store =this.ownerCt.ownerCt.items.items[0].store;
			                     	    store.baseParams ={roleName:this.getValue()};
			          	                store.load({params:{start:0, limit:this.ownerCt.ownerCt.limit}});
		                 	 		}
		                 	 	}
		                 	 }
						
						},'-',{	text: '查询',
							xtype:'button',
							iconCls:'search',
	                     	listeners:{
	                     		click:function(n) {
		                     	    var store =this.ownerCt.ownerCt.items.items[0].store;
			                     	    var combotext = this.ownerCt.items.items[1];
			                     	    store.baseParams ={roleName:this.ownerCt.items.items[1].getValue()};
			          	                store.load({params:{start:0, limit:this.ownerCt.ownerCt.limit}});
	                     		}
	                     	}
			                     	
						}];	
	                this.items= [
	                    {
	                     xtype:'grid',
	                     sm:sm,
						 loadMask:'正在加载数据中，请稍候...'	,
	                	 anchor:'100%',
	                	 height:300,
	                     bbar: new Ext.PagingToolbar({
					            pageSize: this.limit,
					            displayInfo: true,
					            store:mystore,
					            displayMsg: '显示的角色 {0} - {1} 总共 {2}',
					            emptyMsg: "没有找到角色 "
	       					}),	
	                      viewConfig: {
	                       forceFit: true
	                      },
	                      columns:[sm,{header:'角色名称',dataIndex: 'roleName'},
	                      	       {header:'角色描述',dataIndex: 'roleDesc'}],
	                      store:mystore
	                	}],
	                this.buttons= [{
	                    text:'确认',
	                    listeners:{
	                     		click:function(n) {
	                     			var recs =this.ownerCt.ownerCt.getComponent(0).getSelectionModel().getSelections();
	                     			var roleIds ='';
	                     			var pstore =this.ownerCt.ownerCt.parentGrid.getStore();
	                     			for (var a =0;a <recs.length;a++) {
	                     				roleIds =roleIds+','+recs[a].get('roleId');
	                     			}
	                     			if (recs.length!=0) {
	                     				roleIds =roleIds.substring(1)
	                     				var myMask = new Ext.LoadMask(Ext.getBody(), {msg:"正加添加中，请稍候..."});
	                     				myMask.show(this.ownerCt.ownerCt);
	                     				var me =this.ownerCt.ownerCt;
	                     				Ext.Ajax.request({
	                     				      url:'group_addRuleGroupRel.action'
	                     				      ,params:{roleIds:roleIds
	                     				               ,groupId:this.ownerCt.ownerCt.groupId}
	                     				      ,success:function(request,conn) {
	                     				      	 myMask.hide()
	                     				      	 me.close();
	                     				      	 Ext.Msg.alert('提示','添加成功');
	                     				      	 pstore.load();
	                     				      }
	                     				      ,failure:function(){
	                     				      	myMask.hide()
	                     				      }
	                     				});
	                     			} else {
	                     				this.ownerCt.ownerCt.close();
	                     			}
	                     		}
	                    }
	                },{
	                 text:'关闭',
	                 listeners:{
	                 	click:function(n) {
	                 		this.ownerCt.ownerCt.close();
	                 	}
	                 }
	                }]
	                SM.GroupManage.serachRule.superclass.initComponent.call(this);
      }
});

/**
 * 增加组的方法
 * @param {} node
 */
SM.GroupManage.addGroup =function(node,title) {
	 var fn =function(){
	 	var sortNo =0;
			for (var a =0;a <node.childNodes.length;a++){
			  	if (sortNo<node.childNodes[a].attributes.sortNo) {
			  		sortNo =node.childNodes[a].attributes.sortNo
			  	}
	 		}
	  		//添加下级组
		 	new Ext.Window({
			  modal:true
			  ,title:title
			  ,width:340
			  ,height:220
			  ,layout:'form'
			  ,buttons:[{text:'确定',handler:function(){
			  	   var win =this.ownerCt.ownerCt
			       var items =this.ownerCt.ownerCt.items.items;
			       var params="";
			       for (var i=0;i<items.length;i++){
			       	    if (!items[i].isValid()){
			       	    	Ext.Msg.alert('提示',items[i].fieldLabel+items[i].getActiveError());
			       	    	return ;
			       	    }
			       	    params =params+'&smGroup.'+items[i].name+'='+items[i].getValue();
			       }
			       params =params.substring(1)+"&smGroup.parentGroupId="+node.id;
			       
			       Ext.Ajax.request({
		    	       	url:'group_addGroup.action'
		    	       	,params:encodeURI(params)
		    	       	,method:'post'
		    	       	,success:function(response,options){
		    	       		node.reload();
		    	       		win.close();
		    	       	}
				 });
			  }},{text:'取消',handler:function(){
			            this.ownerCt.ownerCt.close();  
			  }}]
			  ,items:[
			  	   {style:{marginTop:'10px'}
			  	        ,labelStyle:"margin-top:10px"
			    		,xtype:'textfield'
			    		,fieldLabel:'组名称'
			    		,allowBlank:false
			    		,width:'200'
			    		,maxLength:200
			    		,name:'groupName'
			    	},{
			    		xtype:'textarea'
			    		,fieldLabel:'组描述'
			    		,width:'200'
			    		,maxLength:400
			    		,allowBlank:false
			    		,name:'groupDesc'
			    	},{
			    		xtype:'numberfield'
			    		,fieldLabel:'序号'
			    		,width:'200'
			    		,maxLength:10
			    		,allowBlank:false
			    		,name:'sortNo'
			    		,value:sortNo+1
			    	}]
		  }).show();
	 }
	 //查看节点是否已经Load ，如果没有Load() 则需要load
	 if (node.isLoaded()){
	 	fn();
	 } else {
	 node.expand(false,false,function(n){
	        node.collapse(false,false);
	         //得到最大sortNo
			fn();
	  });
	 }
	  
}

/**
 * 删除组节点
 * @param {} node
 */
SM.GroupManage.deleteGroup =function(node) {
	  var tree =node.getOwnerTree();
	  
	  Ext.Msg.confirm('确定','您确定删除该节点与所有子节点?',function(sure){
        if (sure=='yes') {
        	var myMask = new Ext.LoadMask(tree.getEl(), {msg:"请稍候,正在删除节点中..."});
			myMask.show();
        	Ext.Ajax.request({
	  				url:'group_delGroupById.action?smGroup.groupId='+node.id
	    	       	,method:'post'
	    	       	,success:function(response,options){
	    	       		node.remove();
	    	       		tree.ownerCt.getComponent(1).clearData();
	    	       		myMask.hide();
	    	       	}
	    	       	,failure:function(){
	            	   		myMask.hide();
	            	 }
	        });
        }
	  });  
}